'''
Created on Jun 7, 2013

@author: Petko
'''
from utils.sententialyzer_client import SententialyzerClient
import json
from opinion import check_opinion_tree, ClientThread, checkAttentions
import unittest
import time
from sententialyzer.settings import SENTENTIALYZER_ADMIN_USER_NAME
import random

class StressTest(unittest.TestCase):
    sc = SententialyzerClient()
    user_id = 0
    
    def create_opinions(self, parents_list, count, first_user_id = 0, title='x', evals = ['positive', 'negative']):
        result = []
        
        class CreateThread(ClientThread):
            def __init__(self, create_id):
                ClientThread.__init__(self, create_id)
                self.commit_delay=2
                
            def operation(self):
                result.append(self.async_sc.createOp(random.choice(parents_list)['id'], random.choice(evals), title=title, commit_pending_ops=False, commit_delay=self.commit_delay))
        
        create_threads = []
        for i in range(count):
            create_threads.append(CreateThread(first_user_id + i))
        
        time_start = time.time()
        
        for ct in create_threads:
            ct.start()
            
        for ct in create_threads:
            ct.join()
        
        print "create_opinions time ", time.time() - time_start, " sec"
        
        return result
    
    def test_simultaneous_creates(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        result = self.sc.createBaseOp("stress test base op", "stress test base op", "moderators:d sent_default_group:rc")
        
        baseOpId = result['id']
        
        self.sc.authorize(SENTENTIALYZER_ADMIN_USER_NAME, 'nopass', 'group_adm view_adm')
        
        self.sc.addOpsToView("Bulgaria", [baseOpId])
        
        op1 = self.sc.authAndCreateOp(baseOpId, baseOpId, 'positive', title = 'op1+', content = 'op plus')
        
        #self.authorize('commenter_sync', 'emptypass', 'all:rc')
        
        OPINIONS_TO_CREATE=100
        
        op11_list = self.create_opinions([op1], OPINIONS_TO_CREATE, title='op11')
        op11_list.append(op1)
        
        self.sc.commit_pending_ops(print_time=True)
        
        checkAttentions(self, op1['id'], -1, -1, OPINIONS_TO_CREATE)
        
        #create and update previous
        op111_list = self.create_opinions(op11_list, OPINIONS_TO_CREATE, title='op111')
        
        self.sc.commit_pending_ops(print_time=True)
        
        checkAttentions(self, op1['id'], -1, -1, OPINIONS_TO_CREATE * 2)
        
        op41_list = self.create_opinions(op111_list, OPINIONS_TO_CREATE, title='op41', first_user_id=OPINIONS_TO_CREATE)
        
        self.sc.commit_pending_ops(print_time=True)
        
        checkAttentions(self, op1['id'], -1, -1, OPINIONS_TO_CREATE * 3)
        
        self.create_opinions(op41_list, OPINIONS_TO_CREATE, title='op41', first_user_id=OPINIONS_TO_CREATE * 2)
        
        self.sc.commit_pending_ops(print_time=True)
        
        checkAttentions(self, op1['id'], -1, -1, OPINIONS_TO_CREATE * 4)
if __name__ == '__main__':
    unittest.main()